package com.peanuts.tender.test;

import com.zhongbaofu.util.Util;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class MacEcbUtils {
	private final static int REQUEST_XML_LEN_SIZE=4;//请求报文长度所占位数
	private final static String WORK_KEY="9999999999999999";//请求报文长度所占位数。。448A4524485442DC、、9999999999999999
	/**
	 * 构造带MAC的请求报文
	 * @param xml
	 * @return
	 * @throws Exception
	 */
	public static String getMacRequestXML(final String xml) throws Exception {

		Document document = null;
		try {
			document = DocumentHelper.parseText(xml);
		} catch (DocumentException e) {
//			logger.error(e.getMessage());
			throw e;
		}
		document.setXMLEncoding("UTF-8");
		Element body = document.getRootElement().element("MBODY");
		String newxml = document.asXML();

		//计算报文长度
		StringBuilder macXML=new StringBuilder(getHexLen(REQUEST_XML_LEN_SIZE,newxml));

		//MAC计算
		String mac = Util.xorAndGenMac(newxml, WORK_KEY);
		System.out.println("mac2 = "+mac);
		body.addElement("MAC").setText(mac);
		macXML.append(document.asXML()) ;

		String result=macXML.toString();

		return result;
	}

	/**
	 * 计算报文长度
	 * @param totalLen
	 * @param requestXML
	 * @return
	 */
	protected static String getHexLen(int totalLen, String requestXML){

		int len=requestXML.length();
		String hexLen=Integer.toHexString(len);


		int cur= totalLen -hexLen.length();

		StringBuilder builder=new StringBuilder();
		for (int i=0;i<cur;i++){
			builder.append("0");
		}

		builder.append(hexLen);

		return builder.toString();
	}





	/**
	 * 6BB43EBC3D90D363
	 * @param args
	 */
	public static void main(String[] args) {
		String context= "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
				"<MESSAGE>\n" +
				"    <MHEAD>\n" +
				"        <VERSION>1.1</VERSION>\n" +
				"        <REQSN>abdh_quick_13522446123</REQSN>\n" +
				"        <PTIME>20180420131645</PTIME>\n" +
				"    </MHEAD>\n" +
				"    <MBODY>\n" +
				"        <BUSINCODE>CDB1705002</BUSINCODE>\n" +
				"        <MERCHANTID>000000000000185</MERCHANTID>\n" +
				"        <IS_BATCH>0</IS_BATCH>\n" +
				"        <POLICYID>18ABIC01003180420348055</POLICYID>\n" +
				"        <ORDERID>dh123456789orderid</ORDERID>\n" +
				"        <PAYINFO>01</PAYINFO>\n" +
				"        <BIZNO>dhyjpaytest1234567890123</BIZNO>\n" +
				"        <AMOUNT>100</AMOUNT>\n" +
				"        <ORDER_INFO>1234568orderinfo</ORDER_INFO>\n" +
				"        <TRADE_DATE>201804201316450</TRADE_DATE>\n" +
				"        <TRADE_TIME>201804201316450</TRADE_TIME>\n" +
				"        <PAY_PRODUCT>PAYECO</PAY_PRODUCT>\n" +
				"        <CHANNELS>QUICK</CHANNELS>\n" +
				"        <CHANNELS_INFO>\n" +
				"            <SMSID>00185011524214910384547627</SMSID>\n" +
				"            <SMSCODE>000000</SMSCODE>\n" +
				"            <NAME>程振东</NAME>\n" +
				"            <BANKCARD>6217730503176040</BANKCARD>\n" +
				"            <MOBILENO>17512525193</MOBILENO>\n" +
				"            <IDCARD>342222199103236412</IDCARD>\n" +
				"        </CHANNELS_INFO>\n" +
				"        <NOTIFY_URL>http://dat.dahuobaoxian.com/paymentcenter/notify/zbf</NOTIFY_URL>\n" +
				"    </MBODY>\n" +
				"</MESSAGE>";
//context = context.replaceAll(" ","").replace("\n","");
//		String key = "9999999999999999";
//		String context = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
//				"<MESSAGE>" +
//				"    <MHEAD>" +
//				"        <PTIME>20180418130838</PTIME>" +
//				"        <VERSION>1.1</VERSION>" +
//				"        <RESPSN>20180418130838126501</RESPSN>" +
//				"        <REQSN>DH_TEST_13522446123</REQSN>" +
//				"    </MHEAD>" +
//				"    <MBODY>" +
//				"        <RTNCODE>ER</RTNCODE>" +
//				"        <RTNMSG>MAC校验失败</RTNMSG>" +
//				"        <BUSINCODE>CDM1705001</BUSINCODE>" +
//
//				"    </MBODY>" +
//				"</MESSAGE>";

		try {
			String result = getMacRequestXML(context);
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}



